package com.ruoyi.wvp.utils;

public class UnicodeConverterUtil {

    public static String stringToUnicode(String str) {
        StringBuilder unicode = new StringBuilder();
        for (char character : str.toCharArray()) {
            // 获取字符的 Unicode 编码
            int code = (int) character;
            // 将 Unicode 编码转换为十六进制表示
            String hexCode = Integer.toHexString(code);
            // 拼接每个字符的 Unicode 编码
            unicode.append("\\u").append(hexCode);
        }
        return unicode.toString();
    }

    public static String unicodeToString(String unicodeString) {
        StringBuilder normalStringBuilder = new StringBuilder();
        for (int i = 0; i < unicodeString.length(); ) {
            if (unicodeString.charAt(i) == '\\' && unicodeString.charAt(i + 1) == 'u') {
                int codePoint = Integer.parseInt(unicodeString.substring(i + 2, i + 6), 16);
                normalStringBuilder.appendCodePoint(codePoint);
                i += 6;
            } else {
                normalStringBuilder.append(unicodeString.charAt(i++));
            }
        }
        return normalStringBuilder.toString();

    }

}

